package cn.nnxy.zentao.controller;


import cn.nnxy.zentao.domain.ZPlan;
import cn.nnxy.zentao.service.PlanService;
import org.apache.catalina.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/*
 * @ build by 何昌舜
 * @ file name 计划controller
 */

@Controller
@RequestMapping("/product")
public class PlanController {
    @Autowired
    private PlanService planService;

    @GetMapping("plan")
    public String planListUI() {
        return "";
    }

    @ResponseBody
    @PostMapping("plan")
    public Map<String, Object> planList(){
        Integer pageSize = 10;
        return null;
    }

    //新建计划
    @GetMapping("planAdd")
    public String planAddUI(){
        return "";
    }

    @PostMapping("planAdd")
    public String planAdd(ZPlan plan, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("plan",plan);
            model.addAttribute("msg", result.getAllErrors().get(0).getDefaultMessage());
            return "";
        }
        int temp = planService.savePlan(plan);
        if (temp == 1){
            model.addAttribute("msg", "添加成功！");
            return "redirect:";
        }else {
            model.addAttribute("msg", "添加失败！");
            return "";
        }

    }

    //删除计划
    @ResponseBody
    @PostMapping("planDel")
    public boolean planDel(Integer[] ids){
        if (null == ids || ids.length == 0){
            return false;
        }
        return planService.deleteById(ids) > 0;
    }

    //编辑计划
    @GetMapping("planEdit")
    public String planEditUI(Integer planId, Model model){
        ZPlan plan = planService.seletById(planId);
        model.addAttribute("plan", plan);
        return "";
    }

    @PostMapping("planEdit")
    public String planEdit(ZPlan plan, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("plan", plan);
            model.addAttribute("msg", result.getAllErrors());
            return "";
        }
        int temp = planService.updateById(plan);
        if (temp == 1){
            model.addAttribute("msg", "修改成功。");
            return "redirect:";
        }else {
            model.addAttribute("msg", "修改失败。");
            return "";
        }

    }

}
